package register;

import android.content.Context;
import android.util.Base64;

import com.irisian.irissdk.EnrollListener;
import com.irisian.irissdk.EnrollResult;
import com.irisian.irissdk.IrisSdk;
import com.irisian.irissdk.ReadDistanceCallBack;

import java.util.ArrayList;
import java.util.List;

import beans.RegisterInfoBean;
import constants.Constants;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.IrisUtil;

/**
 * Created by Administrator on 2018/5/17.
 */

public abstract class IrisRegisterClass {
    public List<EnrollResult> list = new ArrayList<>();
    public MyHandler allHandler;
    private Context mContext;

    public IrisRegisterClass(Context mContext) {
        this.mContext = mContext;
        allHandler = SingletonClass.getSingleton(MyHandler.class);
        IrisUtil.initSdk(mContext);
        IrisSdk.openCamera(mContext);
        IrisSdk.startReadDistance(mContext, new ReadDistanceCallBack() {
            @Override
            public void onReadDistance(int i) {

            }
        });
        startEnroll();
    }

    public void startEnroll() {
        IrisSdk.startEnroll(new EnrollListener() {
            @Override
            public void onEnrollSuccess(EnrollResult enrollResult) {
                synchronized (list) {
                    list.add(enrollResult);
                    if (list.size() == 1) {
                        byte[] code = new byte[IrisSdk.getConfiguration().IRIS_TEMPLATE_LENGTH];
                        System.arraycopy(enrollResult.iris_code, 0, code, 0, IrisSdk.getConfiguration().IRIS_TEMPLATE_LENGTH);
                        String image = Base64.encodeToString(code, Base64.DEFAULT);
                        irisNetWork(image);
                    }
                }
            }

            @Override
            public void onEnrollFailure(boolean b, int i, String s) {
                irisFailure();
            }
        }, 10000);
        IrisSdk.startReadDistance(mContext, new ReadDistanceCallBack() {
            @Override
            public void onReadDistance(int i) {

            }
        });
    }

    public void releaseIris() {
        IrisSdk.stopReadDistance(mContext);
        IrisSdk.closeCamera(mContext);
        IrisSdk.releaseSdk(mContext);
    }

    public abstract void irisNetWork(String image);
    public abstract void irisFailure();
}
